/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package app.form;

import app.modelo.Columna;
import app.modelo.Fila;
import app.modelo.ModeloJTabla;
import app.modelo.Tabla;
import javax.swing.JOptionPane;

/**
 *
 * @author Lain
 */
public class FormRegistrarProyecto extends javax.swing.JFrame {
    
    ModeloJTabla modeloJTabla;
    double costoTotal = 0;
    int totalHoras = 0;

    /**
     * Creates new form FormRegistrarProyecto
     */
    public FormRegistrarProyecto() {
        initComponents();
        Tabla tabla = new Tabla();
        Columna oColumna1 = new Columna("Actividad", "java.lang.String");
        tabla.agregarColumna(oColumna1);
        Columna oColumna2 = new Columna("Horas", "java.lang.Integer");
        tabla.agregarColumna(oColumna2);
        Columna oColumna3 = new Columna("Complejidad", "java.lang.String");
        tabla.agregarColumna(oColumna3);
        Columna oColumna4 = new Columna("Costo", "java.lang.Double");
        tabla.agregarColumna(oColumna4);
        modeloJTabla = new ModeloJTabla(tabla);
        tabActividades.setModel(modeloJTabla);
        txtTotalHoras.setText(String.valueOf(0));
        txtCostoTotal.setText(String.valueOf(0.0));   
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtProyecto = new javax.swing.JTextField();
        txtPresupuesto = new javax.swing.JTextField();
        txtValorHora = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtActividad = new javax.swing.JTextField();
        txtHoras = new javax.swing.JTextField();
        cmbComplejidad = new javax.swing.JComboBox();
        botAgregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabActividades = new javax.swing.JTable();
        botQuitar = new javax.swing.JButton();
        botGuardar = new javax.swing.JButton();
        botSalir = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtTotalHoras = new javax.swing.JTextField();
        txtCostoTotal = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registro de Proyecto");
        setName("FormProyecto"); // NOI18N
        setResizable(false);

        jLabel1.setText("Proyecto:");

        jLabel2.setText("Presupuesto:");

        jLabel3.setText("Valor hora:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 51, 51));
        jLabel4.setText("REGISTRO DE PROYECTO Y SUS ACTIVIDADES");

        jLabel5.setText("Actividad:");

        jLabel6.setText("Horas:");

        jLabel7.setText("Complejidad:");

        cmbComplejidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alta", "Media", "Baja" }));

        botAgregar.setText("Agregar");
        botAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botAgregarActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(tabActividades);

        botQuitar.setText("Quitar");
        botQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botQuitarActionPerformed(evt);
            }
        });

        botGuardar.setText("Guardar");

        botSalir.setText("salir");
        botSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botSalirActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Total horas:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Costo total:");

        txtTotalHoras.setEditable(false);
        txtTotalHoras.setBackground(new java.awt.Color(204, 204, 204));
        txtTotalHoras.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtTotalHoras.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        txtCostoTotal.setEditable(false);
        txtCostoTotal.setBackground(new java.awt.Color(255, 255, 153));
        txtCostoTotal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        txtCostoTotal.setForeground(new java.awt.Color(51, 51, 255));
        txtCostoTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2)
                            .addComponent(jScrollPane1)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txtProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtPresupuesto)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(0, 19, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                                    .addComponent(txtValorHora)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtActividad, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmbComplejidad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addGap(33, 33, 33)
                                .addComponent(jLabel7)
                                .addGap(26, 26, 26))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botAgregar)
                                .addGap(18, 18, 18)
                                .addComponent(botQuitar)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(botGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(botSalir))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTotalHoras, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCostoTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPresupuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtValorHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtActividad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbComplejidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botAgregar)
                    .addComponent(botQuitar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCostoTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtTotalHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(13, 13, 13)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botSalir)
                    .addComponent(botGuardar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void botSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_botSalirActionPerformed

    private void botAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botAgregarActionPerformed
        String proyecto, actividad, complejidad;
        double presupuesto, valorhora, costoActividad;
        int horasActividad;
        
        try{
            proyecto = txtProyecto.getText().trim();
            presupuesto = Double.parseDouble(txtPresupuesto.getText().trim());
            valorhora = Double.parseDouble(txtValorHora.getText().trim());
            actividad = txtActividad.getText().trim();
            horasActividad = Integer.parseInt(txtHoras.getText().trim());
            complejidad = String.valueOf(cmbComplejidad.getSelectedItem());
            costoActividad = valorhora * horasActividad;            
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,"Los datos son incorrectos","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(proyecto.length()==0){
            JOptionPane.showMessageDialog(this,"El nombre del proyecto no se ha ingresado","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(actividad.length()==0){
            JOptionPane.showMessageDialog(this,"El nombre de la actividad no se ha ingresado","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(presupuesto < 1000 || presupuesto > 10000){
            JOptionPane.showMessageDialog(this,"El presupuesto debe ser mayor o igual a S/. 1000.00 \n"
                    + "y menor o igual a $/. 10000.00","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(valorhora < 10){
            JOptionPane.showMessageDialog(this,"El valor hora debe ser mayor o igual a S/. 10.00","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(horasActividad < 4){
            JOptionPane.showMessageDialog(this,"Las horas de la actividad debe ser mayor o igual a 4","Mensaje",JOptionPane.WARNING_MESSAGE);
            return;
        }
        costoTotal += costoActividad;         
        if(costoTotal <= presupuesto){
            totalHoras += horasActividad;
            Fila oFila = new Fila();
            oFila.agregarValor(actividad);
            oFila.agregarValor(horasActividad);
            oFila.agregarValor(complejidad);
            oFila.agregarValor(costoActividad);
            modeloJTabla.agregarFila(oFila);
            txtTotalHoras.setText(String.valueOf(totalHoras));
            txtCostoTotal.setText(String.valueOf(costoTotal));   
            txtActividad.requestFocusInWindow();
        }
        else{
            costoTotal -= costoActividad;
            JOptionPane.showMessageDialog(this,"La actividad no puede agregarse al proyecto. \n"
                    + "El costo total supera el presupuesto","Mensaje",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_botAgregarActionPerformed

    private void botQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botQuitarActionPerformed
        int fila;
        
        fila = tabActividades.getSelectedRow();
        if(fila >= 0){            
            totalHoras -= (Integer)modeloJTabla.getValueAt(fila, 1);
            costoTotal -= (Double)modeloJTabla.getValueAt(fila, 3);
            modeloJTabla.quitarFila(fila);
            txtTotalHoras.setText(String.valueOf(totalHoras));
            txtCostoTotal.setText(String.valueOf(costoTotal)); 
        }
    }//GEN-LAST:event_botQuitarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormRegistrarProyecto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormRegistrarProyecto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormRegistrarProyecto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormRegistrarProyecto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormRegistrarProyecto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botAgregar;
    private javax.swing.JButton botGuardar;
    private javax.swing.JButton botQuitar;
    private javax.swing.JButton botSalir;
    private javax.swing.JComboBox cmbComplejidad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable tabActividades;
    private javax.swing.JTextField txtActividad;
    private javax.swing.JTextField txtCostoTotal;
    private javax.swing.JTextField txtHoras;
    private javax.swing.JTextField txtPresupuesto;
    private javax.swing.JTextField txtProyecto;
    private javax.swing.JTextField txtTotalHoras;
    private javax.swing.JTextField txtValorHora;
    // End of variables declaration//GEN-END:variables
}
